package com.ukao.steward.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ukao.steward.R;
import com.ukao.steward.base.ListBaseAdapter;
import com.ukao.steward.base.SuperViewHolder;
import com.ukao.steward.bean.ProductListBean;
import com.ukao.steward.bean.StorageListBean;
import com.ukao.steward.bean.StringBean;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.L;
import com.ukao.steward.widget.StateButton;
import com.ukao.steward.widget.SwipeMenuView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.ukao.steward.R.drawable.storage_list_pull_down;
import static com.ukao.steward.R.id.bindCode;
import static com.ukao.steward.R.id.bing_btn;
import static com.ukao.steward.R.id.paymentPrice;


public class StorageAdapter extends ListBaseAdapter<StorageListBean.ProListBean> {


    public StorageAdapter(Context context, List<StorageListBean.ProListBean> mDataList) {
        super(context, mDataList);
    }


    @Override
    public int getLayoutId() {
        return R.layout.adapter_storage_item;
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, final int position) {
        //这句话关掉IOS阻塞式交互效果 并依次打开左滑右滑


        LinearLayout layout = holder.getView(R.id.layout);
        TextView isAnnexDescTv = holder.getView(R.id.is_annex_desc_tv);
        LinearLayout takePicturelayout = holder.getView(R.id.take_picture_layout);
        TextView productName = holder.getView(R.id.productName);
        TextView sortNo = holder.getView(R.id.sortNo);
        TextView subtotal = holder.getView(R.id.subtotal);
        TextView bindCode = holder.getView(R.id.bindCode);
        TextView businessName = holder.getView(R.id.businessName);
        TextView annexDesc = holder.getView(R.id.annexDesc);
        TextView picCnt = holder.getView(R.id.picCnt);
        TextView other = holder.getView(R.id.other);
        TextView addServiceDesc = holder.getView(R.id.addServiceDesc);
        StateButton bingBtn = holder.getView(R.id.bing_btn);
        Button copeBtn = holder.getView(R.id.cope_btn);
        Button deleteBtn = holder.getView(R.id.delete_btn);
        StorageListBean.ProListBean item = mDataList.get(position);
        productName.setText(item.getProductName());
        sortNo.setText(item.getNo()+"/"+getDataList().size());
        businessName.setText(item.getBusinessName());
        subtotal.setText( item.getSubtotal()); /* 小计  */
        bindCode.setText(item.getBindCode());
        if (item.isUnfold()){
            setDrawableRight(R.drawable.storage_list_pull_up,subtotal);
        }else{
            setDrawableRight(R.drawable.storage_list_pull_down,subtotal);
        }

        if (CheckUtils.isEmpty(item.getBindCode())){
            bingBtn.setText(R.string.tie_the);
            bingBtn.setNormalBackgroundColor(getColors(R.color.normal_blue));
            bingBtn.setPressedBackgroundColor(getColors(R.color.pressed_blue));
        }else{
            bingBtn.setText(R.string.has_been_tied);
            bingBtn.setNormalBackgroundColor(getColors(R.color.green));
            bingBtn.setPressedBackgroundColor(getColors(R.color.greend_color));
        }
        bingBtn.setVisibility(item.isEnableBindCode()?View.VISIBLE:View.GONE);
        copeBtn.setVisibility(item.isClothing()?View.VISIBLE:View.GONE);
        isAnnexDescTv.setVisibility(item.isClothing()?View.GONE:View.VISIBLE);
        takePicturelayout.setVisibility(item.isClothing()?View.VISIBLE:View.GONE);
        View.OnClickListener  mOnClickListener= v -> mOnItemChildClickListener.onChildItemClick(v.getId(),position,null);
        layout.setOnClickListener(mOnClickListener);
        bingBtn.setOnClickListener(mOnClickListener);
        deleteBtn.setOnClickListener(mOnClickListener);
        copeBtn.setOnClickListener(mOnClickListener);
        takePicturelayout.setOnClickListener(mOnClickListener);
        subtotal.setOnClickListener(mOnClickListener);
        int annexDescCount=0;
        String  annex="";
        Gson mGson=new Gson();
        if (!CheckUtils.isEmpty(item.getAnnexDesc())){  /*附件*/

            List<StringBean>    mAnnexDesclist= mGson.fromJson(item.getAnnexDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());

            StringBuffer mStringBuffer3 = new StringBuffer();
            for (int i = 0; i < mAnnexDesclist.size(); i++) {
                annexDescCount=annexDescCount+1;
                if (i != mAnnexDesclist.size() - 1) {
                    mStringBuffer3.append(mAnnexDesclist.get(i).getName() + "/");
                } else {
                    mStringBuffer3.append(mAnnexDesclist.get(i).getName());
                }


            }
            annex = mStringBuffer3.toString();

        }
        if (!CheckUtils.isEmpty(item.getAddServiceDesc())){  /* 附加  */
            List<StringBean>        mServiceDesclist = mGson.fromJson(item.getAddServiceDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());

            StringBuffer mServiceBuffer = new StringBuffer();
            for (int i = 0; i < mServiceDesclist.size(); i++) {
                StringBean items = mServiceDesclist.get(i);
                if (i != mServiceDesclist.size() - 1) {
                    mServiceBuffer.append(items.getName()).append(CheckUtils.isEmptyNumber(items.getAmt())+ "/");
                } else {
                    mServiceBuffer.append(items.getName()).append(CheckUtils.isEmptyNumber(items.getAmt()));
                }

            }

            addServiceDesc.setText(mServiceBuffer.toString());
            addServiceDesc.setVisibility(item.isUnfold()?View.VISIBLE:View.GONE);

        }else{
            addServiceDesc.setVisibility(View.GONE);
            addServiceDesc.setText(""); /* 附加  */
        }
        annexDesc.setVisibility(annexDescCount != 0?View.VISIBLE:View.GONE);
        annexDesc.setText(annexDescCount != 0 ? annexDescCount + "  " + annex : "");
        picCnt.setText(item.getPicCnt()==0?"":item.getPicCnt()+"");

        StringBuffer mColorBuffer = new StringBuffer();  /*颜色*/
        if (!CheckUtils.isEmpty(item.getColorDesc())){
            List<StringBean>        mColorDesclist = mGson.fromJson(item.getColorDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());


            for (int i = 0; i < mColorDesclist.size(); i++) {
                StringBean items = mColorDesclist.get(i);
                if (i != mColorDesclist.size() - 1) {
                    mColorBuffer.append(items.getName() + "/");
                } else {
                    mColorBuffer.append(items.getName());
                }

            }


        }
        StringBuffer mEffectBuffer = new StringBuffer();  /*洗后效果*/
        if (!CheckUtils.isEmpty(item.getEffectDesc())){
            List<StringBean>        mEffectDesclist = mGson.fromJson(item.getEffectDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());

            for (int i = 0; i < mEffectDesclist.size(); i++) {
                StringBean items = mEffectDesclist.get(i);
                if (i != mEffectDesclist.size() - 1) {
                    mEffectBuffer.append(items.getName() + "/");
                } else {
                    mEffectBuffer.append(items.getName());
                }

            }

        }
        StringBuffer mFlawBuffer = new StringBuffer();/*瑕疵*/
        if (!CheckUtils.isEmpty(item.getFlawDesc())){

            List<StringBean>        mFlawDesclist = mGson.fromJson(item.getFlawDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());

            for (int i = 0; i < mFlawDesclist.size(); i++) {
                StringBean items = mFlawDesclist.get(i);
                if (i != mFlawDesclist.size() - 1) {
                    mFlawBuffer.append(items.getName() + "/");
                } else {
                    mFlawBuffer.append(items.getName());
                }

            }

        }
        String  brandName=item.getBrandName();
        StringBuffer  otherBuffer=new StringBuffer();
        if (!CheckUtils.isEmpty(mColorBuffer.toString())){
            otherBuffer.append(mColorBuffer.toString()).append("/");
        }
        if (!CheckUtils.isEmpty(mEffectBuffer.toString())){
            otherBuffer.append(mEffectBuffer.toString()).append("/");
        }
        if (!CheckUtils.isEmpty(mFlawBuffer.toString())){
            otherBuffer.append(mFlawBuffer.toString()).append("/");
        }

        if (!CheckUtils.isEmpty(brandName)){
            otherBuffer.append(brandName);

        }

            other.setVisibility(item.isUnfold()&&otherBuffer.length()>0?View.VISIBLE:View.GONE);
            other.setText(otherBuffer.toString());


    }
    private    void setDrawableRight(int id,TextView  mTextView) {
        Drawable drawable = mContext.getResources().getDrawable(id);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        mTextView.setCompoundDrawables(null, null, drawable, null);
    }


}
